package com.sunlei.webtest.confirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: sun
 * @Description: 学习rabbitmq的confirm机制
 * @Data: create in 14:48 2018/11/23
 * @Modfied by:
 */
public class Consumer {
    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        System.out.println("-----rabbitmq 消费者 测试comfirm应答机制-----");
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();


        final String exchangeName = "test_confirm_exchange";
        final String routeKey = "confirm.#";
        final String qunueName = "test_confirm_queue";
        channel.exchangeDeclare(exchangeName,"topic",true);

        channel.queueDeclare(qunueName,true,false,false,null);
        channel.queueBind(qunueName,exchangeName,routeKey);

        //创建消费者
        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        channel.basicConsume(qunueName,true,queueingConsumer);

        while (true){
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            String msg = new String(delivery.getBody());
            System.out.println("[consumer 获取消息]: "+msg);
        }
    }
}
